<?php
/**
 * User: itimphp
 * Date: 6/14/12
 * Time: 12:31 AM
 */
require_once '../bootstrap.php';

// Must login
if (empty($_SESSION['admin_id'])) {
	header('Location: login.php');
}
if (!isset($_GET['type'])){
	die('Wrong type to Approve!');
}else{
	$type = $_GET['type'];
}
if ($type == 'league'){
	$league_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

	$selected_league = $my_db->select_row('SELECT isApproved FROM leagues WHERE id = ?', array($league_id));

	if (!empty($selected_league)) {
		$new_approval_status = ($selected_league['isApproved'] + 1) % 2;
		$my_db->execute('UPDATE leagues SET isApproved = ? WHERE id = ?', array($new_approval_status, $league_id));
		echo 'Updated approval status! Redirecting to Leagues List!';
	} else {
		echo "There is no such league in our system! Redirecting to Leagues List!";
	}
	header("Refresh: 5;leagues.php");
}else{
	$ads_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

	$selected_ads = $my_db->select_row('SELECT isApproved FROM ads WHERE adid = ?', array($ads_id));

	if (!empty($selected_ads)) {
		$new_approval_status = ($selected_ads['isApproved'] + 1) % 2;
		$my_db->execute('UPDATE ads SET isApproved = ? WHERE adid = ?', array($new_approval_status, $ads_id));
		echo 'Updated approval status! Redirecting to Ads List!';
	} else {
		echo "There is no such ads in our system! Redirecting to Ads List!";
	}
	header("Refresh: 5;ads_list.php");
}